﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// Writer : billows
/// Time : 2009-07-13 15:30:30
/// Function : 项目中通用的函数
/// </summary>
public class WebUtility
{
    public WebUtility()
    {

    }

    /// <summary>
    /// 生成6位的验证码
    /// </summary>
    /// <returns>Retrun a verify code.</returns>
    public static string RndStr()
    {
        string strCodes = "0,1,2,3,4,5,6,7,8,9";
        string[] Chars = strCodes.Split(',');
        Random rnd = new Random();
        int start = 0;
        int end = 9;
        strCodes = "";
        for (int i = 0; i < 6; i++)
        {
            strCodes += Chars[rnd.Next(start, end)];
            System.Threading.Thread.Sleep(5);
        }
        return strCodes;
    }

    /// <summary>
    /// 产生在线支付的流水号
    /// </summary>
    /// <returns></returns>
    public static string GenrateSerialPayNumber() {
        string num = DateTime.Now.ToString("yyyyMMddHHmm") + new Random().Next(100, 999).ToString();
        return num;
    }

    /// <summary>
    /// 获取md5加密字符
    /// </summary>
    /// <returns>MD5 加密字符串</returns>
    public static string getMd5CryptedStr(string str) {
        string md5 = "";
        if(!string.IsNullOrEmpty(str)){
            md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
        }
        return md5;
    }

    /// <summary>
    /// 生成注册用户成功发送的短信
    /// </summary>
    /// <param name="mobileNO">手机号</param>
    /// <param name="userPwd">用户密码</param>
    /// <param name="cardPoints">卡内点数</param>
    /// <param name="initialText">短信模板</param>
    /// <returns>Return a string value.</returns>
    public static string GenrateSendPurchaseRegMsg(string mobileNO, string userPwd, string cardPoints, string initialText)
    {
        string handleText = "";
        if (!string.IsNullOrEmpty(mobileNO) && !string.IsNullOrEmpty(userPwd) && !string.IsNullOrEmpty(cardPoints))
        {
            bool flag = initialText.Contains("#mobileNO");
            bool flag2 = initialText.Contains("#password");
            bool flag3 = initialText.Contains("#cardPoints");

            if (flag == true && flag2 == true && flag3 == true)
            {
                handleText = initialText.Replace("#mobileNO", mobileNO);
                handleText = handleText.Replace("#password", userPwd);
                handleText = handleText.Replace("#cardPoints", cardPoints);
            }
        }
        return handleText;
    }

    /// <summary>
    /// 弹出提示信息
    /// </summary>
    /// <param name="msg">消息文本</param>
    public void AlertMsg(string msg)
    {
        Page pg = (Page)HttpContext.Current.Handler;
        pg.ClientScript.RegisterStartupScript(this.GetType(), "script", "alert('" + msg + "');", true);
    }

    /// <summary>
    /// 替换危险Sql字符
    /// </summary>
    /// <param name="inputString">要替换的字符</param>
    /// <returns></returns>
    public static string replaceStr(string inputString)
    {
        if (string.IsNullOrEmpty(inputString))
            return "";
        string replaceIn = @";|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|'";
        string[] ArrayReplaceIn = replaceIn.Split(char.Parse("|"));
        int i;
        for (i = 0; i < ArrayReplaceIn.Length; i++)
        {
            inputString = inputString.Replace(ArrayReplaceIn[i].ToString(), "");
        }
        return inputString.Trim();
    }

    /// <summary>
    /// 生成购卡短信息头部
    /// </summary>
    /// <param name="messageNum">购卡卡数</param>
    /// <param name="initialText">短信模板</param>
    /// <returns></returns>
    public static string GenratePurchaseCarsHeaderSms(int messageNum, string initialText)
    {
        string header = "";
        if (!string.IsNullOrEmpty(initialText))
        {
            bool flag = initialText.Contains("#cardNums");
            if(flag == true){
                header = initialText.Replace("#cardNums", messageNum.ToString());
            }
        }
        return header;
    }


    /// <summary>
    /// 生成购卡提示短信文字
    /// </summary>
    /// <param name="initialText"></param>
    /// <returns></returns>
    public static string GenratePurchaseCardSmsText(string cardNo,string cardPassWord,string cardPoints ,string initialText)
    {
        string handleText = "";
        if (!string.IsNullOrEmpty(initialText) && !string.IsNullOrEmpty(cardNo) && !string.IsNullOrEmpty(cardPassWord) && !string.IsNullOrEmpty(cardPoints))
        {
            bool flag = initialText.Contains("#cardNo");
            bool flag2 = initialText.Contains("#cardPassword");
            bool flag3 = initialText.Contains("#cardPoints");
            if (flag == true && flag2 == true && flag3 == true)
            {
                handleText = initialText.Replace("#cardNo", cardNo);
                handleText = handleText.Replace("#cardPassword", cardPassWord);
                handleText = handleText.Replace("#cardPoints", cardPoints);
            }
        }
        return handleText;
    }

    /// <summary>
    /// 购卡历史查询短信头
    /// </summary>
    /// <param name="cardNums">短信条数</param>
    /// <param name="initalText">短信模板</param>
    /// <returns></returns>
    public static string GenratePurchaseCardHistorySmsHeader(int messageNum, string initialText)
    {
        string header = "";
        if (!string.IsNullOrEmpty(initialText))
        {
            bool flag = initialText.Contains("#cardNums");
            if (flag == true)
            {
                header = initialText.Replace("#cardNums", messageNum.ToString());
            }
        }
        return header;
    }

    //购买时间       卡号       金额       咨询点数    支付方式
    //购卡时间：#time,卡号：#cardNo,支付金额：#cardMoney,咨询点数：#cardPoins,支付方式：#paymentType

    /// <summary>
    /// 生成购卡历史短信提示短信
    /// </summary>
    /// <param name="time"></param>
    /// <param name="cardNo"></param>
    /// <param name="cardMoney"></param>
    /// <param name="cardPoints"></param>
    /// <param name="paymentType"></param>
    /// <param name="initialText"></param>
    /// <returns></returns>
    public static string GenratePurchaseCardsHistorySms(string time, string cardNo, string cardMoney, string cardPoints, string paymentType, string initialText) 
    {
        string handleText = "";
        if (!string.IsNullOrEmpty(time) && !string.IsNullOrEmpty(cardNo) && !string.IsNullOrEmpty(cardMoney) && !string.IsNullOrEmpty(cardPoints) && !string.IsNullOrEmpty(initialText) && !string.IsNullOrEmpty(paymentType)) 
        {
            bool flag = initialText.Contains("#time");
            bool flag2 = initialText.Contains("#cardNo");
            bool flag3 = initialText.Contains("#cardMoney");
            bool flag4 = initialText.Contains("#cardPoins");
            bool flag5 = initialText.Contains("#paymentType");

            if (flag == true && flag2 == true && flag3 == true && flag4 == true && flag5 == true) 
            {
                handleText = initialText.Replace("#time", time);
                handleText = handleText.Replace("#cardNo", cardNo);
                handleText = handleText.Replace("#cardMoney", cardMoney);
                handleText = handleText.Replace("#cardPoins", cardPoints);
                handleText = handleText.Replace("#paymentType", paymentType);
            }
        }

        return handleText;
    }

    /// <summary>
    /// 生成注册用户找回密码短信
    /// </summary>
    /// <param name="mobile"></param>
    /// <param name="pwd"></param>
    /// <param name="initalText"></param>
    /// <returns></returns>
    public static string GenreateFindRegUserAccountPwdMsg(string mobile,string pwd,string initalText) 
    { 
        string handleText = "";
        if (!string.IsNullOrEmpty(mobile) && !string.IsNullOrEmpty(pwd) && !string.IsNullOrEmpty(initalText)) 
        {
            bool flag = initalText.Contains("#mobileNo");
            bool flag2 = initalText.Contains("#regPwd");

            if(flag == true && flag2 == true)
            {
                handleText = initalText.Replace("#mobileNo", mobile);
                handleText = handleText.Replace("#regPwd", pwd);
            }
        }
        return handleText;
    }

    //------------------------------------------
    /// <summary>
    /// 生成发送咨询卡密码短信
    /// </summary>
    /// <param name="cardNO">卡号</param>
    /// <param name="cardPwd">卡密码</param>
    /// <param name="initialText">短信模板</param>
    /// <returns>Return a string value.</returns>
    public static string GenrateSendSelectCardMsg(string cardNO, string cardPwd, string initialText)
    {
        string handleText = "";
        if (!string.IsNullOrEmpty(cardNO) && !string.IsNullOrEmpty(cardPwd))
        {
            bool flag = initialText.Contains("#cardNo");
            bool flag2 = initialText.Contains("#cardPassword");

            if (flag == true && flag2 == true)
            {
                handleText = initialText.Replace("#cardNo", cardNO);
                handleText = handleText.Replace("#cardPassword", cardPwd);
            }
        }
        return handleText;
    }

    /// <summary>
    /// 将列项信息绑定到相应的列项中
    /// </summary>
    /// <param name="colName">列项名称</param>
    /// <param name="ddl">控件名称</param>
    public static void BindList(string colName, DropDownList ddl)
    {
        BLL.ReadParamtes bllParam = new BLL.ReadParamtes();
        ddl.DataSource = bllParam.GetColsInfo(colName);
        ddl.DataTextField = "Content";
        ddl.DataValueField = "Code";
        ddl.DataBind();
        //ddl.Items.Insert(0, new ListItem("", ""));
    }

    /// <summary>
    /// 截取字符串
    /// </summary>
    /// <param name="strChar">要截取字符串</param>
    /// <param name="intLong">截取长度</param>
    /// <returns>Return a string value.</returns>
    public static string Left(string strChar, int intLong)
    {
        string val = strChar;
        int lon = intLong;
        int len = val.Length;
        if (lon < len)
        {
            val = val.Substring(0, lon) + "......";
        }
        return val;
    }


    public static string GenrateSendVerifyCodeMsgTemp(string code, string initialText)
    {
        string handleText = "";
        if (!string.IsNullOrEmpty(code))
        {
            bool flag = initialText.Contains("#verifyCode");

            if (flag == true)
            {
                handleText = initialText.Replace("#verifyCode", code);
            }
        }
        return handleText;
    }

    /// <summary>
    /// 获取根路径
    /// </summary>
    public static string getWebRootPath()
    {
        string WebsiteRoot = "";
        string strAppPath = "";
        string UrlAuthority = System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
        string appPath = HttpContext.Current.Request.ApplicationPath;
        if (appPath == "\\")
            strAppPath = UrlAuthority;
        else
            strAppPath = UrlAuthority + appPath;
        WebsiteRoot = strAppPath;

        return WebsiteRoot;
    }



}
